package com.lebang.service;

import com.lebang.mapper.SysCommonMapper;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class SysCommonService {

    @Autowired
    private SysCommonMapper commonMapper;


    public List<Map<String, Object>> listByQuery(String table, int pageSize, int startNum, Map<String, String> queryMap, Map<String, String> timeMap, Map<String, String> searchMap, String orderBy) {
        return commonMapper.listByQuery(table, pageSize, startNum, queryMap, timeMap, searchMap, orderBy);
    }


    public long countByQuery(String table, Map<String, String> queryMap, Map<String, String> timeMap, Map<String, String> searchMap) {
        return commonMapper.countByQuery(table, queryMap, timeMap, searchMap);
    }

    public boolean deletedData(String table, String ids) {
        List<Integer> idList = new ArrayList<>();
        if (ids.contains(",")) {
            String[] temp = ids.split("[,]");
            for (String str : temp) {
                idList.add(Integer.valueOf(str));
            }
        } else {
            idList.add(Integer.valueOf(ids));
        }
        return commonMapper.deletedData(table, idList) > 0;
    }
    public JsonData enablebyid(Integer id, String status, String table) {
        Integer i = commonMapper.enablebyid(id, status, table);
        if (i > 0) {
            return Callback.success("200", "操作成功!", null);
        } else {
            return Callback.fail("403", "操作失败!", null);
        }

    }


    public JsonData commonSwitch(Integer id, String status, String table, String feild) {
        Integer i = commonMapper.commonSwitch(id, status, table, feild);
        if (i > 0) {
            return Callback.success("200", "操作成功!", null);
        } else {
            return Callback.fail("403", "操作失败!", null);
        }
    }


}
